package com.linkwings.dbTemp;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import javax.servlet.http.HttpServletRequest;


import org.apache.log4j.Logger;
public class WebUtils {

	//使用log4j初始化
	protected Logger log = Logger.getLogger(this.getClass());


	public static List<String> getURLParamsList(HttpServletRequest request,String[] excludeParams){
		List<String> list=new ArrayList<String>();
		Enumeration paras = request.getParameterNames();
		String paraName = "";
		if(excludeParams!=null&&excludeParams.length>0){
			boolean hasExcPara=false;
			while (paras.hasMoreElements()) {
				paraName = (String) paras.nextElement();
				hasExcPara=false;
				for(String excPara:excludeParams){
					if(paraName.equalsIgnoreCase(excPara)){
						hasExcPara=true;
						break;
					}
				}
				if(hasExcPara==false&&list.contains(paraName)==false){
				list.add(paraName);
				}
			}
		}else{
			while (paras.hasMoreElements()) {
				paraName = (String) paras.nextElement();
				if(list.contains(paraName)==false){
				list.add(paraName);
				}
			}
		}
		return list;
	}

	public static String getTrueURL(HttpServletRequest request,String[] excludeParams){
		String url=request.getRequestURI()+"?";
		Enumeration paras = request.getParameterNames();
		String paraName = "";
		if(excludeParams!=null&&excludeParams.length>0){
			boolean hasPara=false;
			while (paras.hasMoreElements()) {
				paraName = (String) paras.nextElement();
				hasPara=false;
				for(String excPara:excludeParams){
					if(paraName.equalsIgnoreCase(excPara)){
						hasPara=true;
						break;
					}
				}
				if(hasPara==false){
					url=url+paraName+"="+request.getParameter(paraName)+"&";
				}
			}
		}else{
			while (paras.hasMoreElements()) {
				paraName = (String) paras.nextElement();
				url=url+paraName+"="+request.getParameter(paraName)+"&";
			}
		}
		url=url.substring(0, url.length()-1);
		return url;
	}

	public static String getGmcPrivilegeStyleLink(HttpServletRequest request){
		String url=request.getRequestURI();
		String method=request.getParameter("method");
		if(method!=null){
			url=url+"?method="+method;
		}
		url=url.substring(url.indexOf("/", 1));
		return url;
	}

	public static String changeParamsToSqlForm(String[] params){
		if(params!=null&&params.length>0){
			String str="";
			for(int i=0;i<params.length;i++){
				str=str+params[i];
				if(i<params.length-1){
					str=str+",";
				}
			}
			return str;
		}else{
			return null;
		}
	}

	public static void main(String[] args){
		String a=changeParamsToSqlForm(new String[]{"0","1","2","3"});
		String b=null;
		if(b==null||b.equals("")==false)
		System.out.print(a);
	}

}
